package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.SalaryForm;
import com.amsp.oa.oademo.service.SalaryFormService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/salary")
@Api(tags = "职员基本薪资核定表管理")
public class SalaryFormController {

    @Autowired
    private SalaryFormService salaryFormService;

    @PostMapping("/save")
    @ApiOperation("保存薪资核定表")
    public ResponseEntity<?> save(@RequestBody SalaryForm salaryForm) {
        try {
            log.info("接收到的薪资核定表: {}", salaryForm);

            // 基本数据验证
            if (salaryForm.getName() == null || salaryForm.getName().trim().isEmpty()) {
                return ResponseEntity.badRequest().body("姓名不能为空");
            }
            if (salaryForm.getEmployeeId() == null || salaryForm.getEmployeeId().trim().isEmpty()) {
                return ResponseEntity.badRequest().body("工号不能为空");
            }

            boolean result = salaryFormService.saveOrUpdate(salaryForm);
            if (result) {
                return ResponseEntity.ok(true);
            } else {
                return ResponseEntity.badRequest().body("保存失败");
            }
        } catch (Exception e) {
            log.error("保存薪资核定表失败", e);
            return ResponseEntity.badRequest().body("保存失败: " + e.getMessage());
        }
    }

    @GetMapping("/view/{id}")
    @ApiOperation("查看薪资核定表详情")
    public ResponseEntity<?> getById(@PathVariable Long id) {
        try {
            SalaryForm record = salaryFormService.getById(id);
            if (record != null) {
                return ResponseEntity.ok(record);
            } else {
                return ResponseEntity.badRequest().body("未找到记录");
            }
        } catch (Exception e) {
            log.error("获取薪资核定表失败", e);
            return ResponseEntity.badRequest().body("获取失败: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    @ApiOperation("查询薪资核定表列表")
    public ResponseEntity<?> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String employeeId) {
        try {
            Page<SalaryForm> page = new Page<>(current, size);
            QueryWrapper<SalaryForm> queryWrapper = new QueryWrapper<>();

            if (name != null && !name.isEmpty()) {
                queryWrapper.like("name", name);
            }
            if (employeeId != null && !employeeId.isEmpty()) {
                queryWrapper.like("employee_id", employeeId);
            }

            queryWrapper.orderByDesc("id");
            return ResponseEntity.ok(salaryFormService.page(page, queryWrapper));
        } catch (Exception e) {
            log.error("查询薪资核定表列表失败", e);
            return ResponseEntity.badRequest().body("查询失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除薪资核定表")
    public ResponseEntity<?> delete(@PathVariable Long id) {
        try {
            boolean result = salaryFormService.removeById(id);
            if (result) {
                return ResponseEntity.ok(true);
            } else {
                return ResponseEntity.badRequest().body("删除失败");
            }
        } catch (Exception e) {
            log.error("删除薪资核定表失败", e);
            return ResponseEntity.badRequest().body("删除失败: " + e.getMessage());
        }
    }
}